package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.PerformanceRequestBody;
import com.example.elderlycare.dao.entity.Performance;
import com.example.elderlycare.service.PerformanceService;
import com.example.elderlycare.service.RosterService;
import com.example.elderlycare.service.entity.PerformanceItem;
import com.example.elderlycare.service.entity.QueryListResponseBody;
import com.example.elderlycare.service.entity.RosterStatisticsResponseBody;
import com.example.elderlycare.utils.DateUtil;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/perform")
public class PerformanceController {
    @Resource
    RosterService rosterService;

    @Resource
    PerformanceService performanceService;

    @GetMapping("/do")
    public Result<?> doPerform(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "name", required = false) String name
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("name", name == null ? null : "%" + name + "%");

        QueryListResponseBody<RosterStatisticsResponseBody> responseBody = rosterService.queryRosterStatisticsList(params);

        List<RosterStatisticsResponseBody> items = responseBody.getItems();
        AtomicInteger count = new AtomicInteger();
        items.forEach(item -> {
            try {
                Performance performance = Performance.builder().
                        employeeId(item.getEmployeeId()).
                        countRoster(item.getCountAll()).
                        countAbsent(item.getCountAbsent()).
                        countLeave(item.getCountLeave()).
                        rosterBase(10).
                        absentBase(10).
                        leaveBase(10).
                        award(0).
                        punish(item.getCountAbsent() * 10 + item.getCountLeave() * 10).
                        nowPer(item.getCountAll() * 10 - (item.getCountAbsent() * 10 + item.getCountLeave() * 10)).
                        originPer(item.getCountAll() * 10 - (item.getCountAbsent() * 10 + item.getCountLeave() * 10)).
                        timeDate((DateUtil.getDateFromString(startTime))).build();
                boolean success = performanceService.save(performance);
                if (!success) {
                    count.getAndIncrement();
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
        if (count.intValue() != 0) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @DeleteMapping
    public Result<?> deletePerform(@RequestParam(value = "id") Integer id) {
        boolean success = performanceService.removeById(id);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @PutMapping
    public Result<?> updatePerform(@Valid @RequestBody PerformanceRequestBody.UpdatePerformanceRequestBody req) {
        Performance performance = Performance.builder().
                id(req.getId()).
                rosterBase(req.getRosterBase()).
                absentBase(req.getAbsentBase()).
                leaveBase(req.getLeaveBase()).
                award(req.getAward()).
                punish(req.getCountAbsent() * req.getAbsentBase() + req.getCountLeave() * req.getLeaveBase()).
                nowPer(req.getCountRoster() * req.getRosterBase() - (req.getCountAbsent() * req.getAbsentBase() + req.getCountLeave() * req.getLeaveBase())).
                build();
        boolean success = performanceService.updateById(performance);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @GetMapping
    public Result<QueryListResponseBody<PerformanceItem>> queryPerform(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("name", name == null ? null : "%" + name + "%");
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        QueryListResponseBody<PerformanceItem> responseBody = performanceService.queryListResponseBody(params);
        return Result.success(responseBody);
    }
}
